﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using NCKH.models;
namespace NCKH.Test
{
    public class Methods
    {
        public static List<string> Times = new List<string>();
        public static List<string> Days = new List<string>();
        public static int Space = new int();
        public static void WriteHoursList(string pathFile, XmlDocument xmlDoc)
        {
            Times.Clear();
              var  chtg = new EXM_CauHinhThoiGian
                {
                    Kieu_thoi_gian = "120",
                    Ngay_thoi_gian = "1111110",
                    Thoi_gian_bat_dau = "0730,0930,0000,1330,1530"
                };
            string[] hours = chtg.Thoi_gian_bat_dau.Split(',');

            for (int i = 0; i < hours.Length; i++)
                hours[i] = hours[i].Insert(2, ":");

            int space = Space = int.Parse(chtg.Kieu_thoi_gian);

            //----Hours

            XmlNode root = xmlDoc.SelectSingleNode("descendant::fet/Hours_List");
            {
                XmlNode nodeNumber = xmlDoc.CreateNode(XmlNodeType.Element, "Number", null);
                nodeNumber.InnerText = ((hours.Length > 5 ? 5 : hours.Length)).ToString();
                root.AppendChild(nodeNumber);
                for (int i = 0; i < (hours.Length > 5 ? 5 : hours.Length); i++)
                {
                    if (hours[i].Equals("00:00"))
                    {
                        XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, "Name", null);
                        node.InnerText = "Nghỉ";
                        root.AppendChild(node);
                        //-----------------------------------------------------
                        Times.Add("Nghỉ");
                    }
                    else
                    {
                        XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, "Name", null);
                        TimeSpan ts = TimeSpan.Parse(hours[i]);
                        string hour = (DateTime.Parse(ts.ToString())).ToString("HH:mm");
                        ts += (new TimeSpan(0, space, 0));
                        hour = String.Format("{0} - {1}", hour, (DateTime.Parse(ts.ToString())).ToString("HH:mm"));
                        node.InnerText = hour;
                        root.AppendChild(node);
                        //-----------------------------------------------------
                        Times.Add(hour);
                    }
                }
            }
            xmlDoc.Save(pathFile);
        }
        public static void WriteDaysList(string pathFile, XmlDocument xmlDoc)
        {
            Days.Clear();
              var  chtg = new EXM_CauHinhThoiGian
                {
                    Kieu_thoi_gian = "120",
                    Ngay_thoi_gian = "1111110",
                    Thoi_gian_bat_dau = "0730,0930,0000,1330,1530"
                };
            char[] tmp = chtg.Ngay_thoi_gian.ToCharArray();

            string[] days = new string[tmp.Length];

            int number = 0;

            for (int i = 0; i < days.Length; i++)
            {
                days[i] = tmp[i].ToString();

                if (days[i].Equals("1"))
                {
                    number++;
                    days[i] = (i != days.Length - 1) ? "Thứ " + (i + 2) : "Chủ nhật";
                }
            }
            //-------------------------------------------------------------------------------------------------------------

            XmlNode root = xmlDoc.SelectSingleNode("descendant::fet/Days_List");
            {
                XmlNode nodeNumber = xmlDoc.CreateNode(XmlNodeType.Element, "Number", null);
                nodeNumber.InnerText = (number).ToString();
                root.AppendChild(nodeNumber);

                for (int i = 0; i < days.Length; i++)
                    if (!days[i].Equals("0"))
                    {
                        XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, "Name", null);
                        node.InnerText = days[i];
                        root.AppendChild(node);
                        //----------------------------------------------------
                        Days.Add(days[i]);
                    }
            }
            xmlDoc.Save(pathFile);
        }
        public static void WriteStudentsList(string pathFile, XmlDocument xmlDoc)
        {
            Context db = new Context();

            //List<SCH_Nhom_SV> students = db.SCH_Nhom_SVs.Where(w => w.Ky_dang_ky == Program.DataSchedule.Ky_dang_ky).ToList();
            var students = db.EXM_ToChucThiPhongs.ToList();
            XmlNode root = xmlDoc.SelectSingleNode("descendant::fet/Students_List");
            {
                //var lvl1s = students.Where(w => w.ID_mon == 0);

                foreach (var lvl1 in students)
                {
                    XmlNode nodeYear = xmlDoc.CreateNode(XmlNodeType.Element, "Group", null);
                    {
                        XmlNode node1 = xmlDoc.CreateNode(XmlNodeType.Element, "Name", null);
                        node1.InnerText = lvl1.ID_phong_thi_1.ToString();
                        nodeYear.AppendChild(node1);
                        //-------------------------------------------------------
                        node1 = xmlDoc.CreateNode(XmlNodeType.Element, "Number_of_Students", null);
                        node1.InnerText = lvl1.Sosv.ToString();
                        nodeYear.AppendChild(node1);
                        //-------------------------------------------------------                    
                    }
                    root.AppendChild(nodeYear);
                }
            }
            xmlDoc.Save(pathFile);
        }
        public static void WriteSubjectList(string pathFile, XmlDocument xmlDoc)
        {
            Context db = new Context();

            var subjects = db.EXM_ToChucThiPhongs.Select(a => a.ID_mon).Distinct().ToList();

            XmlNode root = xmlDoc.SelectSingleNode("descendant::fet/Subjects_List");
            {
                foreach (var subject in subjects)
                {
                    XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, "Subject", null);
                    XmlNode nodeLeaf = xmlDoc.CreateNode(XmlNodeType.Element, "Name", null);
                    nodeLeaf.InnerText = subject.ToString();
                    node.AppendChild(nodeLeaf);
                    root.AppendChild(node);
                }
            }
            xmlDoc.Save(pathFile);
        }
        public static void WriteBuildingsList(string pathFile, XmlDocument xmlDoc)
        {
            Context db = new Context();

            var buildings = db.PLAN_TOANHAs.Select(s => s.ID_nha).ToList();
            //-------------------------------------------------------------------------------------------------------------

            XmlNode root = xmlDoc.SelectSingleNode("descendant::fet/Buildings_List");
            {
                foreach (var building in buildings)
                {
                    XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, "Building", null);
                    XmlNode nodeLeaf = xmlDoc.CreateNode(XmlNodeType.Element, "Name", null);
                    nodeLeaf.InnerText = building.ToString();
                    node.AppendChild(nodeLeaf);
                    root.AppendChild(node);
                }
            }
            xmlDoc.Save(pathFile);
        }
        public static void WriteRoomsList(string pathFile, XmlDocument xmlDoc)
        {
            Context db = new Context();

            var rooms = db.PLAN_PhongHocs.Select(s => new { s.ID_nha, s.ID_phong, s.Suc_chua }).ToList();

            //-------------------------------------------------------------------------------------------------------------

            XmlNode root = xmlDoc.SelectSingleNode("descendant::fet/Rooms_List");
            {
                foreach (var room in rooms)
                {
                    XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, "Room", null);
                    {
                        XmlNode nodeLeaf = xmlDoc.CreateNode(XmlNodeType.Element, "Name", null);
                        nodeLeaf.InnerText = room.ID_phong.ToString();
                        node.AppendChild(nodeLeaf);
                        //-------------------------------------------------------
                        nodeLeaf = xmlDoc.CreateNode(XmlNodeType.Element, "Building", null);
                        nodeLeaf.InnerText = room.ID_nha.ToString();
                        node.AppendChild(nodeLeaf);
                        //-------------------------------------------------------
                        nodeLeaf = xmlDoc.CreateNode(XmlNodeType.Element, "Capacity", null);
                        nodeLeaf.InnerText = room.Suc_chua.ToString();
                        node.AppendChild(nodeLeaf);
                    }
                    root.AppendChild(node);
                }
            }
            xmlDoc.Save(pathFile);
        }
        public static void WriteActivityTagsList(string pathFile, XmlDocument xmlDoc)
        {
            Context db = new Context();

            var activityTags = db.EXM_HinhThucThis.Select(s => s.ID_nhan).Distinct().ToList();
            //-------------------------------------------------------------------------------------------------------------

            XmlNode root = xmlDoc.SelectSingleNode("descendant::fet/Activity_Tags_List");
            {
                foreach (var activityTag in activityTags)
                {
                    XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, "Activity_Tag", null);
                    XmlNode nodeLeaf = xmlDoc.CreateNode(XmlNodeType.Element, "Name", null);
                    nodeLeaf.InnerText = activityTag.ToString();
                    node.AppendChild(nodeLeaf);
                    root.AppendChild(node);
                }
            }
            xmlDoc.Save(pathFile);
        }
        public static void WriteActivitiesList( string pathFile,XmlDocument xmlDoc)
        {
            XmlNode root = xmlDoc.SelectSingleNode("descendant::fet/Activities_List");
            {
                Context db = new Context();
                var sukiens = db.EXM_SuKienPhongThis.ToList();
                foreach (var row in sukiens)
                {
                    XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, "Activity", null);
                    {
                        XmlNode nodeLeaf = xmlDoc.CreateNode(XmlNodeType.Element, "Subject", null);
                        nodeLeaf.InnerText = (row.ID_mon).ToString();
                        node.AppendChild(nodeLeaf);
                        //-------------------------------------------------------
                        //{
                        //    var activityTag = row.RangBuoc.Nhan_buoi_giang == null ? "1" : row.RangBuoc.Nhan_buoi_giang;
                        //    nodeLeaf = xmlDoc.CreateNode(XmlNodeType.Element, "Activity_Tag", null);
                        //    nodeLeaf.InnerText = activityTag;
                        //    node.AppendChild(nodeLeaf);
                        //}

                        nodeLeaf = xmlDoc.CreateNode(XmlNodeType.Element, "Activity_Tag", null);
                        nodeLeaf.InnerText = row.Hinh_thuc_thi ? "1" : "0";
                        node.AppendChild(nodeLeaf);

                        //-------------------------------------------------------
                        nodeLeaf = xmlDoc.CreateNode(XmlNodeType.Element, "Duration", null);
                        nodeLeaf.InnerText = row.So_tiet_thi.ToString();
                        node.AppendChild(nodeLeaf);
                        nodeLeaf = xmlDoc.CreateNode(XmlNodeType.Element, "Total_Duration", null);
                        nodeLeaf.InnerText = row.So_tiet_thi.ToString();
                        node.AppendChild(nodeLeaf);
                        //-------------------------------------------------------
                        nodeLeaf = xmlDoc.CreateNode(XmlNodeType.Element, "Id", null);
                        nodeLeaf.InnerText = row.ID.ToString();
                        node.AppendChild(nodeLeaf);
                        //-------------------------------------------------------
                        nodeLeaf = xmlDoc.CreateNode(XmlNodeType.Element, "Activity_Group_Id", null);
                        nodeLeaf.InnerText = "0";
                        node.AppendChild(nodeLeaf);
                    }
                    root.AppendChild(node);
                }
            }
            xmlDoc.Save(pathFile);
        }
    }
}
